package br.com.portalnet.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.portalnet.model.Equipamento;
import br.com.portalnet.model.Parametro;
import br.com.portalnet.util.HibernateUtil;

public class ParametroDAO implements IParametroDAO{

	@Override
	public void save(Parametro parametro) {
		Session session = null;
		
		try
		{
		
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.save(parametro);
			t.commit();
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
		
	}

	@Override
	public Parametro getParametro(long id) {
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();	
			Parametro param = (Parametro) session.get(Parametro.class,id);
			param.getEquipamento().getId();			
			return param;
		}finally 
		{
			
			try
			{
				
				
				if(session != null)
				{
					session.close();
				}
			
				
			} catch (Exception e) {
				
			}
		
		}
	}

	@Override
	public List<Parametro> list() {
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			//List lista = session.createQuery("from Livro").list();		
			List lista = session.createCriteria(Parametro.class).list();
			t.commit();
			return lista;
			
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			return null;
		}
		
		finally 
		{
			session.close();
			
		
		}
		
		
	}

	@Override
	public void remove(Parametro parametro) {
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.delete(parametro);
			t.commit();
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
		
	}

	@Override
	public void update(Parametro parametro) {
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.update(parametro);
			t.commit();
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
		
	}

	@Override
	public List<Parametro> listParamByEquip(Equipamento equip) {

		Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			
			Criteria criteria = session.createCriteria(Parametro.class);
			criteria.add(Restrictions.eq("equipamento", equip));
			//List lista = session.createQuery("from Livro").list();
			
			List<Parametro> lista =  criteria.list();
			t.commit();
			return lista;
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
			return null;
			
		}
		
		finally 
		{
			session.close();
			
		
		}
	}

	@Override
	public Parametro getParametroByNome(String nome) {
Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			
			Criteria criteria = session.createCriteria(Parametro.class);
			criteria.add(Restrictions.eq("nome", nome));
			//List lista = session.createQuery("from Livro").list();		
			Parametro param = (Parametro) criteria.list().get(0);
			
			param.getEquipamento().getId();
			
			t.commit();
			return param;
			
		}
		catch(Exception erro)
		{
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
			return null;
			
		}
		
		finally 
		{
			session.close();
			
		
		}
	}

}
